Rmarkdown Example

Document Author

2017-05-28

Diamonds Dataset

使用 knitr::kable() 展示靜態圖表。

knitr::kable(head(diamonds))
carat cut color clarity depth table price x y z
0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48

使用 DT::datatable() 展示動態圖表。

DT::datatable(diamonds)

Exploratory Data Analysis

knitr::kable(summary(diamonds))
carat cut color clarity depth table price x y z
Min. :0.200 Fair : 1610 D: 6775 SI1 :13065 Min. :43.0 Min. :43.0 Min. : 326 Min. : 0.00 Min. : 0.00 Min. : 0.00
1st Qu.:0.400 Good : 4906 E: 9797 VS2 :12258 1st Qu.:61.0 1st Qu.:56.0 1st Qu.: 950 1st Qu.: 4.71 1st Qu.: 4.72 1st Qu.: 2.91
Median :0.700 Very Good:12082 F: 9542 SI2 : 9194 Median :61.8 Median :57.0 Median : 2401 Median : 5.70 Median : 5.71 Median : 3.53
Mean :0.798 Premium :13791 G:11292 VS1 : 8171 Mean :61.8 Mean :57.5 Mean : 3933 Mean : 5.73 Mean : 5.73 Mean : 3.54
3rd Qu.:1.040 Ideal :21551 H: 8304 VVS2 : 5066 3rd Qu.:62.5 3rd Qu.:59.0 3rd Qu.: 5324 3rd Qu.: 6.54 3rd Qu.: 6.54 3rd Qu.: 4.04
Max. :5.010 NA I: 5422 VVS1 : 3655 Max. :79.0 Max. :95.0 Max. :18823 Max. :10.74 Max. :58.90 Max. :31.80
NA NA J: 2808 (Other): 2531 NA NA NA NA NA NA
diamonds %>% 
    group_by(cut) %>% 
    summarise(mean_depth = mean(depth)) %>% 
    knitr::kable()
cut mean_depth
Fair 64.04
Good 62.37
Very Good 61.82
Premium 61.26
Ideal 61.71
# create a new variable "mean_depth"
mean_depth <- diamonds %>% 
            group_by(cut) %>% 
            summarise(mean_depth = mean(depth))

The average depth of “Very Good” cut diamonds is 61.8183. # Data Visualization

g <- mean_depth %>% 
    ggplot(aes(x = cut, y = mean_depth)) +
    geom_bar(stat = "identity")
g

Data Modeling

tree_model <- rpart(cut ~ depth, data = diamonds)
plot(as.party(tree_model))